﻿using NetPageCommon;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace WebDemo.Controllers
{
    public class NoticeController : Controller
    {
        public IActionResult Index(int page=1, int size=5)
        {

            int total = 0;

            SqlSugarClient db = DbHelper.GetNewDb();
            List<Notice> notices = db.Queryable<Notice>().ToPageList(page,size,ref total);
            

            PageModel<Notice> pageModel = new PageModel<Notice>();
            
            var request = HttpContext.Request;

            pageModel.url =  "/Notice/Index";
            pageModel.list = notices;
            pageModel.page = page;
            pageModel.size = size;
            pageModel.total = total; 
            return View(pageModel);
        }


        public IActionResult Edit(string id)
        {
            SqlSugarClient db = DbHelper.GetNewDb();
            Notice notice = db.Queryable<Notice>().Where(n => n.Id == id).Single();
            return View(notice);
        }



        [HttpPost]
        public IActionResult Update(Notice nce)
        {
            ResponseResult<Notice> res = new ResponseResult<Notice>();
            SqlSugarClient db = DbHelper.GetNewDb();

            Notice notice = db.Queryable<Notice>().Where(n => n.Id == nce.Id).Single();
            notice.content += "修改后";
            db.Updateable<Notice>(notice).ExecuteCommand();


            long id = SnowFlakeSingle.instance.getID();
            res.Message = "更新成功";
            res.Status = ResultStatus.Success;
            //res.Data = newEntity;
            return Json(res);

            return Json(notice);

        }


        public IActionResult Add()
        {
            return View();
        }



        [HttpPost]
        public IActionResult Insert(Notice nce)
        {
            ResponseResult<Notice> res = new ResponseResult<Notice>();

            SqlSugarClient db = DbHelper.GetNewDb();


            var from = HttpContext.Request;

            var title = from.Form["title"];
            var content = from.Form["content"];


            long id = SnowFlakeSingle.instance.getID();
            nce.Id = id.ToString();
            //db.Insertable<Notice>(nce).ExecuteCommand();
            StaticConfig.Check_StringIdentity = false;


            var newEntity = db.Insertable<Notice>(nce).ExecuteReturnEntity();

            long id2 = SnowFlakeSingle.instance.getID();
            nce.Id = id2.ToString();
            //db.Insertable<Notice>(nce).ExecuteCommand();


            res.Message = "添加成功";
            res.Status = ResultStatus.Success;
            res.Data = newEntity;
            return Json(res);

        }


        [HttpPost]
        public IActionResult Delete(Notice nce)
        {
            ResponseResult<Notice> res = new ResponseResult<Notice>();
            SqlSugarClient db = DbHelper.GetNewDb();


            Notice notice = db.Queryable<Notice>().Where(n => n.Id == nce.Id).Single();
           var result =  db.Deleteable<Notice>(notice).ExecuteCommand();

            //db.Deleteable<Notice>().In(nce.Id).ExecuteCommand();

            
            
            res.Message = "删除成功";
            res.Status = ResultStatus.Success;
            
            return Json(res);


        }




    }
}
